const config = require("../config");
const model = require("../model");
const Table = model.Table;

module.exports = {
  create: async (tableData) => {
    let result = await Table.create(tableData);
    if (result) {
      return {
        "result": true,
        "data": result
      };
    } else return { "result": false, }
  },

  getById: async (tableId) => {
    let result = await Table.findById(tableId);
    if (result) {
      return {
        "result": true,
        "data": result
      };
    } else return { "result": false, }
  },

  getAll: async () => {
    let result = await Table.findAll({
      where: {}
    });
    if (result) {
      return {
        "result": true,
        "data": result
      };
    } else return { "result": false, }
  },

  deleteById: async (id) => {
    if (await Table.destroy({ where: { id: id } })) {
      return {
        "result": true,
      };
    } else return { "result": false, }
  },

  updateStructById: async (id, struct) => {
    let result = await Table.update({
      struct: struct
    }, {
        where: { id: id }
      });
    if (result[0] === 1) {
      return {
        "result": true,
      };
    } else return { "result": false, }
  },

  updateTitleById: async (id, title) => {
    let result = await Table.update({
      title: title
    }, {
        where: { id: id }
      });
    if (result[0] === 1) {
      return {
        "result": true,
      };
    } else return { "result": false, }
  },

  updateInfoById: async (id, info) => {
    let result = await Table.update({
      info: info
    }, {
        where: { id: id }
      });
    if (result[0] === 1) {
      return {
        "result": true,
      };
    } else return { "result": false, }
  },


}
